﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObserverModel2
{
    public interface IWeatherData
    {
         void AddObserver(IShowBord showbord);
         void RemoveObserver(IShowBord showbord);
         void NotifyObserver();
         void SetTransData(TransData data);
    }

    public interface IShowBord
    {
         void Update(TransData data);
         void Display(TransData data);
    }

    public class TransData
    {
        public int A{get;set;}
        public int B{get;set;}
        public int C{get;set;}
    }

    public class WeatherData : IWeatherData
    {
        #region IWeatherData Members
        public IList<IShowBord> observers = new List<IShowBord>();
        TransData data = null;
        public void AddObserver(IShowBord showbord)
        {
            observers.Add(showbord);
        }

        public void RemoveObserver(IShowBord showbord)
        {
            observers.Remove(showbord);
        }

        public void NotifyObserver()
        {
            foreach (IShowBord observer in observers)
            {
                if (data != null)
                {
                    observer.Update(data);
                }
            }
        }

        public void SetTransData(TransData data)
        {
            this.data = data;
            this.NotifyObserver();
        }

        #endregion
    }

    public class ShowBordA : IShowBord
    {
        public ShowBordA(IWeatherData weatherdata)
        {
            if (weatherdata != null)
            {
                weatherdata.AddObserver(this);
            }
        }
        #region IShowBord Members
        public void Update(TransData data)
        {
            this.Display(data);
        }

        public void Display(TransData data)
        {
            Console.WriteLine("Bord A={0}, B={1}, C={2}", data.A, data.B, data.C);
            Console.WriteLine();
        }
        #endregion
    }

    public class ShowBordB : IShowBord
    {
        #region IShowBord Members
        public void Update(TransData data)
        {
            this.Display(data);
        }

        public void Display(TransData data)
        {
            Console.WriteLine("Max :{0}",Math.Max(Math.Max(data.A, data.B), data.C));
            Console.WriteLine();
        }
        #endregion
    }
    public class ShowBordC : IShowBord
    {
        #region IShowBord Members
        public void Update(TransData data)
        {
            this.Display(data);
        }

        public void Display(TransData data)
        {
            Console.WriteLine("Average :{0}", (data.A+data.B+data.C)/3.0);
            Console.WriteLine();
        }
        #endregion
    }



    class Program2
    {
        static void Main2(string[] args)
        {
            WeatherData wd = new WeatherData();
            IShowBord a = new ShowBordA(wd);
            IShowBord b = new ShowBordB();
            IShowBord c = new ShowBordC();
            wd.AddObserver(a);
            wd.AddObserver(b);
            wd.AddObserver(c);

            TransData data = new TransData();
            data.A = 4;
            data.B = 8;
            data.C = 1;

            wd.SetTransData(data);
            Console.ReadLine();
        }
    }
}
